﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using MINERP.BLL.InfoConfig;
using MINERP.DAL;
using MINERP.MODEL.InfoConfig;

namespace MINERP.InfoConfig.Controller
{
    public  class DelegateController
    {
        #region //private fields
        private PaperFaceController _paperFaceController = new PaperFaceController();
        private PaperTypeController _paperTypeController = new PaperTypeController();
        private ProductController _productController = new ProductController();
        private DelegateBLL _bll = new DelegateBLL();
        private GridControl _grid = null;
        private GridView _view = null;
        private ComboBoxEdit _comboBox = null;
        private GroupControl _groupControl = null;
        private CheckEdit _checkEdit = null;

        #endregion


        #region //Public Properties

        internal GroupControl GroupControl
        {
            set
            {
                _groupControl = value;
            }
        }
        internal CheckEdit CheckEdit
        {
            set
            {
                _checkEdit = value;
            }
        }
        internal ComboBoxEdit ComoBox
        {
            set
            {
                _comboBox = value;
            }
        }
        internal GridControl Grid
        {
            set
            {
                _grid = value;
            }
        }

        internal GridView View
        {
            set
            {
                this._view = value;
            }
            get
            {
                return _view;
            }
        }

        #endregion

        #region //Public Methods

        /// <summary>
        /// Get shops when the form loadding
        /// </summary>
        public void GetDate()
        {
            _grid.DataSource = _bll.GetOutProcessShopList();
        }
        /// <summary>
        /// Get shop object by shop id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OutProcessShop GetOutProcessShopObj(int id)
        {
            return _bll.GetOutProcessShopObj(id);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="list"></param>
        public void AddOutProcessShop(OutProcessShop obj, List<ProductEntity> list)
        {
            foreach (var item in list)
            {
                ProductBLL productBLL = new ProductBLL();
                Tb_OutShop_ProductCategory osp = new Tb_OutShop_ProductCategory();
                osp.UnitPrice = (decimal)item.UnitPrice;
                osp.NeedTime = item.NeedTime;
                osp.IsDefaultOutShop = item.IsProductDefaultShop;
                osp.Tb_Product = productBLL.GetProductObj(item.ProuctId);
                obj.Tb_OutShop_ProductCategory.Add(osp);
            }
            _bll.Add(obj);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="obj"></param>
        public void UpdateOutProcessShop(OutProcessShop obj, List<ProductEntity> list,List<ProductEntity> lisNoChecked)
        {

            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                OutProcessShop ops = entities.OutProcessShop.FirstOrDefault(o=>o.OutShopId== obj.OutShopId);
                ops.Addr = obj.Addr;
                ops.City = obj.City;
                ops.Code = obj.Code;
                ops.EmailAddress = obj.EmailAddress;
                ops.IsUse = obj.IsUse;
                ops.Keeper = obj.Keeper;
                ops.OutShopName = obj.OutShopName;
                ops.Remark = obj.Remark;
                ops.Tel1 = obj.Tel1;
                ops.Tel2 = obj.Tel2;
                ops.AutoInGrid = obj.AutoInGrid;
                if (!ops.Tb_OutShop_ProductCategory.IsLoaded)
                {
                    ops.Tb_OutShop_ProductCategory.Load();
                }
                foreach (var item in ops.Tb_OutShop_ProductCategory)
                {
                    if (!item.Tb_ProductReference.IsLoaded)
                    {
                        item.Tb_ProductReference.Load();
                    }
                }
                List<Tb_Product> listProduct = new List<Tb_Product>();
                if (ops != null)
                {
                    foreach (var item in list)
                    {
                        Tb_Product product = entities.Tb_Product.FirstOrDefault(p=>p.ProuctId ==item.ProuctId);
                        foreach (var ospc in ops.Tb_OutShop_ProductCategory)
                        {

                            listProduct.Add(ospc.Tb_Product);
                        }
                        if (listProduct.Contains(product, new c()))
                        {
                            Tb_OutShop_ProductCategory opcTemp = entities.Tb_OutShop_ProductCategory.FirstOrDefault(ospProduct => ospProduct.ProductId == product.ProuctId && ospProduct.OutShopId==obj.OutShopId);
                            opcTemp.NeedTime = item.NeedTime;
                            opcTemp.UnitPrice = (decimal)item.UnitPrice;
                            opcTemp.IsDefaultOutShop = item.IsProductDefaultShop;
                            
                        }
                        else
                        {
                            Tb_OutShop_ProductCategory opc = new Tb_OutShop_ProductCategory();
                            opc.UnitPrice = (decimal)item.UnitPrice;
                            opc.NeedTime = item.NeedTime;
                            opc.Tb_Product = product;
                            opc.IsDefaultOutShop = item.IsProductDefaultShop;
                            ops.Tb_OutShop_ProductCategory.Add(opc);
                        }
                    }
                    foreach (var item in lisNoChecked)
                    {
                        foreach (var product in listProduct)
                        {
                            if (item.ProuctId == product.ProuctId)
                            {
                                Tb_OutShop_ProductCategory opcTemp = entities.Tb_OutShop_ProductCategory.FirstOrDefault(ospProduct => ospProduct.ProductId == product.ProuctId && ospProduct.OutShopId == obj.OutShopId);
                                if (opcTemp != null)
                                {
                                    ops.Tb_OutShop_ProductCategory.Remove(opcTemp);
                                }
                            }
                        }
                    }
                    
                }
                entities.SaveChanges();
            }
        }

        public class c : IEqualityComparer<Tb_Product>
        {
            public bool Equals(Tb_Product x, Tb_Product y)
            {
                if (x.ProuctId == y.ProuctId)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            //public int GetHashCode(tb_Action a)
            //{
            //    return 0;
            //}

            #region IEqualityComparer<Tb_Product> 成员


            public int GetHashCode(Tb_Product obj)
            {
            
                return 0;
            }

            #endregion
        }
        /// <summary>
        /// Delete shop
        /// </summary>
        /// <param name="obj"></param>
        public void DeleteOutProcessShop(OutProcessShop obj)
        {
            _bll.Delete(obj);
        }

        #endregion

        internal OutProcessShop GetOutProcessShopObj(string p)
        {
            return _bll.GetOutProcessShopObj(p);
        
        }

        /// <summary>
        /// 检查该产品是否已存在默认的委外商
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public bool ProductHasDefaultOutShop(int productId,out OutProcessShop outShop)
        {
            return _bll.ProductHasDefaultOutShop(productId, out outShop);
        }

        internal void ChangeDefaultOutShop(int producId,int outShopId)
        {
            _bll.ChangeDefaultOutShop(producId,outShopId);
        }
        internal void ProductActiveCode(int outshopId)
        {
            _bll.ProductActiveCode(outshopId);
        }
        
    }
}
